﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-06-09 14:13:18
 * 功能：激励广告
**************************************************************************************/

using Game.Framework;

public class Reward : AdBase
{
    public Reward(string adUnitId) : base(adUnitId)
    {
        m_AdType = EAdType.RewardedVideo;
    }

    public override void Initialize()
    {
        MaxSdkCallbacks.Rewarded.OnAdLoadedEvent += OnLoadedEvent;
        MaxSdkCallbacks.Rewarded.OnAdLoadFailedEvent += OnLoadFailedEvent;
        MaxSdkCallbacks.Rewarded.OnAdDisplayedEvent += OnDisplayedEvent;
        MaxSdkCallbacks.Rewarded.OnAdDisplayFailedEvent += OnAdFailedToDisplayEvent;
        MaxSdkCallbacks.Rewarded.OnAdClickedEvent += OnClickedEvent;
        MaxSdkCallbacks.Rewarded.OnAdHiddenEvent += OnHiddenEvent;
        MaxSdkCallbacks.Rewarded.OnAdRevenuePaidEvent += OnAdRevenuePaidEvent;

        LoadAd();
    }

    protected override void LoadAd()
    {
        base.LoadAd();
        MaxSdk.LoadRewardedAd(m_AdUnitId);
        LogManager.Log($"加载Max广告，type:{m_AdType}", "广告");
    }

    protected override void OnAdFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo, MaxSdkBase.AdInfo adInfo)
    {
        // 加了h5后去掉这行代码，解开下面注释的代码
        base.OnAdFailedToDisplayEvent(adUnitId, errorInfo, adInfo);
        //LoadAd();
        //LogManager.Log($"Max广告显示失败，type:{m_AdType}，adUnitId:{adUnitId} code:{errorInfo.Code} networkErrorCode{errorInfo.MediatedNetworkErrorCode} msg:{errorInfo.MediatedNetworkErrorMessage} info:{errorInfo.AdLoadFailureInfo}", "广告");
        //NativeInteraction.Instance.ShowRewardAd_Self();
    }

    protected override void OnAdRevenuePaidEvent(string adUnitId, MaxSdkBase.AdInfo info)
    {
        m_TotalReward = GameDataMgr.Instance.RewardAdTotalRevenue;
        base.OnAdRevenuePaidEvent(adUnitId, info);
        GameDataMgr.Instance.RewardAdTotalRevenue = m_TotalReward;
    }

    /// <summary>显示激励广告</summary>
    public override void Show(ETriggerAdType type)
    {
        m_TriggerType = type;
        if (IsReady())
        {
            EventCenter.Instance.Trigger(GameEventName.ON_BE_ABOUT_TO_SHOW_AD, m_AdType, m_TriggerType);
            MaxSdk.ShowRewardedAd(m_AdUnitId);
            LogManager.Log($"开始Max激励广告播放,adUnitID:{m_AdUnitId}", "广告");
        }
        else
        {
            //UIUtility.DisplayPanel(Game.UI.LoadingAdPanel.LoadingAdPanelView.PANEL_KEY, "UI/LoadingAdPanel/Prefabs/LoadingAdPanel", data: type);
            LogManager.Log($"Max激励广告未准备好，无法播放！！", "广告");
        }
    }

    public bool IsReady() => MaxSdk.IsRewardedAdReady(m_AdUnitId);
}